<template>
  <div>
    <div class="item_title">设备状态</div>
    <div
      v-for="(item, index) of devicstausdata"
      :key="index"
      :class="index | styleFilter"
    >
      {{ item.devname }}
    </div>
    <div class="deviceStatus_circle"></div>
    <div ref="deviceStatus" style="width: 100%; height: 250px" />
  </div>
</template>
<script>
import ajax from "@/utils/ajax";
import echarts from "echarts";

let settime = null;
let devicstausdatalg;
export default {
  data() {
    return {
      devicstausdata: [
      ],
      num: 1,
    };
  },
  created() {},
  mounted() {
    this.getdevicstausdata()
  },
  beforeDestroy() {
    clearInterval(settime);
  },

  filters: {
    styleFilter(val) {
      return "deviceStatus-carousel_" + "abcdef".charAt(val);
    },
  },
  methods: {
    getdevicstausdata() {
      ajax.get({
        url: this.$API.getDevState,
        data: {},
        success: (response) => {
          let self = this
          self.devicstausdata=response.data
          self.devicstausdatalg = self.devicstausdata.length;
          self.getDeviceStatusChart(this.devicstausdata[0]);

          clearInterval(settime);
          settime = setInterval((res) => {
            self.getDeviceStatusChart(self.devicstausdata[self.num]);
            if (self.num < self.devicstausdatalg - 1) {
              self.num++;
            } else {
              self.num = 0;
            }
          }, 5000);
        },
      });
    },
    //房间使用 echarts图表
    getDeviceStatusChart(data) {
      const chart = this.$refs.deviceStatus;
      if (chart) {
        const myChart = echarts.init(chart);
        const option = {
          // 颜色循环
          color: ["#7ee8f4", "#d948b3"],
          title: {
            subtext:
              "{a|正常}\n{b|" +
              data.normal +
              "}\n{a|异常}\n{b|" +
              data.abnormal +
              "}",
            top: "65",
            left: "-160",
            subtextStyle: {
              rich: {
                a: {
                  fontSize: 18,
                  color: "#e0e0e0",
                  width: "60",
                  align: "center",
                },
                b: {
                  fontSize: 18,
                  color: "#fff",
                  width: "60",
                  height: "30",
                  align: "center",
                },
              },
            },
          },
          // 绘制图像
          series: [
            {
              name: "",
              type: "pie",
              radius: ["60%", "70%"],
              center: ["140", "50%"], //图的位置，距离左跟上的位置
              // 图例扩展
              label: {
                show: false,
                position: "top",
                formatter: "{b} : {d}%",
                color: "#ffffff",
              },
              data: [
                {
                  name: "正常",
                  value: data.normal,
                },
                {
                  name: "异常",
                  value: data.abnormal,
                },
              ],
            },
          ],
        };
        myChart.setOption(option);
        window.addEventListener("resize", function () {
          myChart.resize();
        });
      }
      this.$on("hook:destroyed", () => {
        window.removeEventListener("resize", function () {
          myChart.resize();
        });
      });
    },
  },
};
</script>
<style scoped>
* {
  box-sizing: border-box;
}
.item_title {
  height: 50px;
  padding: 15px;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  border-bottom: 1px solid rgba(72, 143, 255, 0.2784313725490196);
}
.deviceStatus_circle {
  position: absolute;
  left: 42px;
  top: 78px;
  width: 196px;
  height: 196px;
  border-radius: 98px;
  background: #202c5f;
  border: 1px solid #7ee8f4;
}

.deviceStatus-carousel_a {
  background: linear-gradient(to right, #66ffff, #66ffff00);
  width: 120px;
  height: 30px;
  color: #ebebeb;
  position: absolute;
  left: 200px;
  top: 160px;
  line-height: 30px;
  padding-left: 45px;
  animation: AGo 30s linear infinite;
  animation-fill-mode: both;
}
@keyframes AGo {
  0% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  14% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  17% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  30% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  33% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  47% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  50% {
    left: 170px;
    top: 240px;
    opacity: 0;
  }
  64% {
    left: 170px;
    top: 80px;
    opacity: 0;
  }
  67% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  80% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  83% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  97% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  100% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
}
.deviceStatus-carousel_b {
  background: linear-gradient(to right, #66ffff, #66ffff00);
  width: 120px;
  height: 30px;
  color: #ebebeb;
  position: absolute;
  left: 190px;
  top: 130px;
  line-height: 30px;
  padding-left: 45px;
  animation: BGo 30s linear infinite;
  animation-fill-mode: both;
}
@keyframes BGo {
  0% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  14% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  17% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  30% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  33% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  47% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  50% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  64% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  67% {
    left: 170px;
    top: 240px;
    opacity: 0;
  }
  80% {
    left: 170px;
    top: 80px;
    opacity: 0;
  }
  83% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  97% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  100% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
}
.deviceStatus-carousel_c {
  background: linear-gradient(to right, #66ffff, #2eacb900);
  width: 120px;
  height: 30px;
  color: #ebebeb;
  position: absolute;
  left: 180px;
  top: 100px;
  line-height: 30px;
  padding-left: 45px;
  animation: CGo 30s linear infinite;
  animation-fill-mode: both;
}
@keyframes CGo {
  0% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  14% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  17% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  30% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  33% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  47% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  50% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  64% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  67% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  80% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  83% {
    left: 170px;
    top: 240px;
    opacity: 0;
  }
  97% {
    left: 170px;
    top: 80px;
    opacity: 0;
  }
  100% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
}

.deviceStatus-carousel_d {
  background: linear-gradient(to right, #66ffff, #66ffff00);
  width: 120px;
  height: 30px;
  color: #ebebeb;
  position: absolute;
  left: 170px;
  top: 80px;
  opacity: 0;
  line-height: 30px;
  padding-left: 45px;
  animation: DGo 30s linear infinite;
  animation-fill-mode: both;
}
@keyframes DGo {
  0% {
    left: 170px;
    top: 80px;
    opacity: 0;
  }
  14% {
    left: 170px;
    top: 80px;
    opacity: 0;
  }
  17% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  30% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  33% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  47% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  50% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  64% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  67% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  80% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  83% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  97% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  100% {
    left: 170px;
    top: 240px;
    opacity: 0;
  }
}

.deviceStatus-carousel_e {
  background: linear-gradient(to right, #66ffff, #66ffff00);
  width: 120px;
  height: 30px;
  color: #ebebeb;
  position: absolute;
  left: 180px;
  top: 220px;
  line-height: 30px;
  padding-left: 45px;
  animation: EGo 30s linear infinite;
  animation-fill-mode: both;
}
@keyframes EGo {
  0% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  14% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  17% {
    left: 170px;
    top: 240px;
    opacity: 0;
  }
  30% {
    left: 170px;
    top: 80px;
    opacity: 0;
  }
  33% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  47% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  50% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  64% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  67% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  80% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  83% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  97% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  100% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
}
.deviceStatus-carousel_f {
  background: linear-gradient(to right, #66ffff, #66ffff00);
  width: 120px;
  height: 30px;
  color: #ebebeb;
  position: absolute;
  left: 190px;
  top: 190px;
  line-height: 30px;
  padding-left: 45px;
  animation: FGo 30s linear infinite;
  animation-fill-mode: both;
}
@keyframes FGo {
  0% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  14% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
  17% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  30% {
    left: 180px;
    top: 220px;
    opacity: 0.4;
  }
  33% {
    left: 170px;
    top: 240px;
    opacity: 0;
  }
  47% {
    left: 170px;
    top: 80px;
    opacity: 0;
  }
  50% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  64% {
    left: 180px;
    top: 100px;
    opacity: 0.4;
  }
  67% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  80% {
    left: 190px;
    top: 130px;
    opacity: 0.7;
  }
  83% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  97% {
    left: 200px;
    top: 160px;
    opacity: 1;
  }
  100% {
    left: 190px;
    top: 190px;
    opacity: 0.7;
  }
}
</style>